# -*- coding: utf-8 -*-
import configparser
import os
from apps.gm.globalmanger import GlobalManager


class ConfigInfo:
    def __init__(self):
        print("当前路径为:", os.getcwd())
        self.module_conf_path = os.path.join(os.getcwd(), "apps/cfg/module_config.ini")
        self.host_conf_path = os.path.join(os.getcwd(), "apps/cfg/host_config.ini")
        # 这个根据host配置参数个数修改
        self.cfg_para_count = 6

        self.gm = GlobalManager()
        self.get_all_info()

    def get_host_dic_info(self, path):
        # 获取section节点
        ip_list = []
        dic_info = {}
        config = self.get_cfg_sections(path)
        sections = config.sections()
        for sec in sections:
            list_info = []
            ip = ''
            for key, val in config.items(sec):
                if str(val) != '':
                    list_info.append(val)
                    if str(key) == 'ip':
                        ip = val
            if len(list_info) == self.cfg_para_count:
                dic_info[ip] = list_info
                ip_list.append(ip)
        self.gm.set_dic_to_dic(1, dic_info)
        self.gm.set_list_to_list(1, ip_list)

    def get_moudle_dic_info(self, path):
        # 获取section节点
        knc_name_dic = {}
        qnc_name_dic = {}
        nc_name_list = []
        md_name_dic = {}
        self.gm.manager_ip = []

        config = self.get_cfg_sections(path)
        sections = config.sections()
        for sec in sections:
            for key, val in config.items(sec):
                if sec == "kncname":
                    knc_name_dic[key] = val
                    nc_name_list.append(val)
                elif sec == "qncname":
                    qnc_name_dic[key] = val
                    nc_name_list.append(val)
                elif sec == "mdname":
                    md_name_dic[key] = val
                elif sec == "mip":
                    self.gm.manager_ip.append(val)
                elif sec == "jhnc":
                    self.gm.jhnc_list.append(val)
                elif sec == "jsnc":
                    self.gm.jsnc_list.append(val)
                elif sec == "ccnc":
                    self.gm.ccnc_list.append(val)
                else:
                    pass
        self.gm.set_dic_to_dic(2, knc_name_dic)
        self.gm.set_dic_to_dic(3, qnc_name_dic)
        self.gm.set_dic_to_dic(4, md_name_dic)
        self.gm.set_list_to_list(2, nc_name_list)

    def get_cfg_sections(self, path):
        config = configparser.ConfigParser()
        config.read(path, encoding="utf-8")
        return config

    def get_module_info(self):
        return self.get_moudle_dic_info(self.module_conf_path)

    def get_host_info(self):
        return self.get_host_dic_info(self.host_conf_path)

    def get_all_info(self):
        self.get_host_dic_info(self.host_conf_path)
        self.get_moudle_dic_info(self.module_conf_path)


if __name__ == '__main__':
    cfg = ConfigInfo()
    gm = GlobalManager()
    cfg.get_all_info()
    print(gm.manager_ip)